package vincent.com.layouttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import vincent.com.layouttest.service.MessengerService;
import vincent.com.layouttest.util.LogUtil;

/**
 * Created by zhengwenchen on 2017/1/17.
 */

public class ActStartModeActivity extends ColorStatusActivity {

    private boolean mRegisterReceiver = false;
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if(MessengerService.ACTION_GOTO_MAIN_PAGE.equals(action)){
                LogUtil.i("czw", "前台回到 主页");
                Intent startIntent = new Intent(ActStartModeActivity.this, DrawerTopOnActionBar.class);
                startIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(startIntent);
            }
        }
    };

    @Override
    public View onCreateView(ViewGroup rootView) {
        return LayoutInflater.from(this).inflate(R.layout.activity_act_start_mode, rootView, true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(!mRegisterReceiver){
            IntentFilter filter = new IntentFilter();
//            filter.addAction(MessengerService.ACTION_GOTO_MAIN_PAGE);
            mRegisterReceiver = true;
            registerReceiver(mReceiver, filter);
        }
    }

    public void gotoMainPage(View view){
        Intent broadcast = new Intent(MessengerService.ACTION_GOTO_MAIN_PAGE);
        sendBroadcast(broadcast);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mRegisterReceiver){
            unregisterReceiver(mReceiver);
            mRegisterReceiver = false;
        }
    }
}
